package org.company.management.utilities;

import java.util.Calendar;
import java.util.List;

import org.company.management.controller.GanttController;
import org.eclipse.nebula.widgets.ganttchart.GanttChart;
import org.eclipse.nebula.widgets.ganttchart.GanttEvent;
import org.eclipse.nebula.widgets.ganttchart.GanttGroup;
import org.eclipse.nebula.widgets.ganttchart.GanttPhase;
import org.eclipse.nebula.widgets.ganttchart.GanttSection;
import org.eclipse.nebula.widgets.ganttchart.GanttSpecialDateRange;
import org.eclipse.nebula.widgets.ganttchart.IGanttEventListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;

/**
 * @author Paner
 *
 */
public class CustomGanttEventListener implements IGanttEventListener
{
	private GanttChart _ganttChart;
	
	public CustomGanttEventListener(GanttChart ganttChart)
	{
		if(ganttChart == null)
		{
			//
		}
		_ganttChart = ganttChart;
	}

	@Override
	public void eventDoubleClicked(GanttEvent arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventHeaderSelected(Calendar arg0, List arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventMovedToNewSection(GanttEvent arg0, GanttSection arg1,
			GanttSection arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventPropertiesSelected(List arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventReordered(GanttEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventSelected(GanttEvent arg0, List arg1, MouseEvent arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventsDeleteRequest(List arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventsDroppedOrResizedOntoUnallowedDateRange(List arg0,
			GanttSpecialDateRange arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventsMoveFinished(List arg0, MouseEvent arg1) {
		try
		{
			GanttController.updateLastDate(_ganttChart);
			
			GanttEvent movedEvent = (GanttEvent)arg0.get(0);
			Calendar cal = movedEvent.getActualStartDate();
			for(Object event : _ganttChart.getGanttComposite().getEvents())
			{
				GanttEvent ganttEvent = (GanttEvent)event;
				
				if(!movedEvent.equals(ganttEvent))
				{
					if(ganttEvent.getY() >= movedEvent.getY()-3 && ganttEvent.getY() <= movedEvent.getY()+3)
					{
						if(ganttEvent.getGanttGroup() != null)
						{
							ganttEvent.getGanttGroup().addEvent(movedEvent);
						}
						else
						{
							GanttGroup newGroup = new GanttGroup(_ganttChart);
							newGroup.addEvent(ganttEvent);
							newGroup.addEvent(movedEvent);
						}
						//isGrouped = true;
						break;
					}
	
					GanttGroup newGroup = new GanttGroup(_ganttChart);
					newGroup.addEvent(movedEvent);
				
				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	@Override
	public void eventsMoved(List arg0, MouseEvent arg1) {
		
		
	}

	@Override
	public void eventsResizeFinished(List arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventsResized(List arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void lastDraw(GC arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void phaseMoveFinished(GanttPhase arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void phaseMoved(GanttPhase arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void phaseResizeFinished(GanttPhase arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void phaseResized(GanttPhase arg0, MouseEvent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void zoomReset() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void zoomedIn(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void zoomedOut(int arg0) {
		// TODO Auto-generated method stub
		
	}

}
